﻿package com._public._ui 
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * 集合按钮
	 * @author WZH(shch8.com)
	 */
	public class SprBtn extends Sprite
	{
		private var _upState:DisplayObject
		private var _overState:DisplayObject
		private var _downState:DisplayObject
		
		public function SprBtn(upState:DisplayObject,overState:DisplayObject,downState:DisplayObject) 
		{
			_upState = upState;
			_overState = overState;
			_downState = downState;
			this.addEventListener(Event.REMOVED, remove)
			setBtnPro(_upState as Sprite)
			setBtnPro(_overState as Sprite)
			setBtnPro(_downState as Sprite)
			_overState.visible = _downState.visible = false;
			this.graphics.beginFill(0, 0)
			this.graphics.drawRect(0, 0, width, height);
			this.graphics.endFill();
			addEventListener(MouseEvent.MOUSE_OVER, mouseEvent)
			addEventListener(MouseEvent.MOUSE_OUT, mouseEvent)
			addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent)
			addEventListener(MouseEvent.MOUSE_UP, mouseEvent)
		}
		private function mouseEvent(e:MouseEvent):void {
			_upState.visible=_overState.visible=_downState.visible=false
			switch(e.type) {
				case MouseEvent.MOUSE_OVER:
				_overState.visible = true;
				break
				case MouseEvent.MOUSE_OUT:
				_upState.visible = true;
				break
				case MouseEvent.MOUSE_UP:
				_overState.visible = true;
				break
				case MouseEvent.MOUSE_DOWN:
				_downState.visible = true;
				break
			}
		}
		private function remove(e:Event):void {
			this.removeEventListener(Event.REMOVED, remove)
			removeEventListener(MouseEvent.MOUSE_OVER, mouseEvent)
			removeEventListener(MouseEvent.MOUSE_OUT, mouseEvent)
			removeEventListener(MouseEvent.MOUSE_DOWN, mouseEvent)
			removeEventListener(MouseEvent.MOUSE_UP, mouseEvent)
		}
		private function setBtnPro(spr:Sprite):void {
			if (spr == null) return;
			spr.mouseChildren = false;
			spr.mouseEnabled = false;
			spr.tabEnabled = false;
			spr.tabChildren = false;
			spr.buttonMode = false;
			addChild(spr)
		}
	}
	
}